package com.mashibing.test.case7.base.full.condition;

import com.mashibing.test.case7.entities.colors.Green;
import com.mashibing.test.case7.entities.colors.Pink;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试类:
 * org.springframework.context.annotation.ImportSelectorTests
 * 链接:
 * 1.【spring】DeferredImportSelector的使用与源码分析
 *  https://blog.csdn.net/u022812849/article/details/123729249
 * 2.spring笔记-DeferredImportSelector
 * https://www.jianshu.com/p/58f8c2f37e12
 * 3.Spring笔记-@Import实现Enable系列注解
 * https://www.jianshu.com/p/5f99d0809eaa
 */
public class MyDeferredImportSelector implements DeferredImportSelector {

	@Override
	public Class<? extends Group> getImportGroup() {
		return DeferredImportSelectorBeanGroup.class;
	}

	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		return new String[]{Green.class.getName(), Pink.class.getName()};
	}

	private static class DeferredImportSelectorBeanGroup implements DeferredImportSelector.Group {

		private final List<Entry> instanceImports = new ArrayList<>();

		@Override
		public void process(AnnotationMetadata metadata, DeferredImportSelector selector) {
			for (String importClassName : selector.selectImports(metadata)) {
				this.instanceImports.add(new Entry(metadata, importClassName));
			}
		}

		@Override
		public Iterable<Entry> selectImports() {
			return instanceImports;
		}
	}
}
